package com.blog.blogvue.controller;

/**
 * @author tangqj
 * @Description 登录接口
 * @create 2020/1/18 15:55
 */

import com.blog.admin.service.SysUserService;
import com.blog.admin.vo.SysRoleVO;
import com.blog.admin.vo.SysUserInfomationVO;
import com.blog.admin.vo.SysUserVO;
import com.blog.common.ApiIntercept;
import com.blog.common.SimpleCharVerifyCodeGenService;
import com.blog.util.PasswordUtil;
import com.blog.util.RedisUtil;
import com.blog.util.Result;
import com.blog.util.TokenUtil;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

@Controller
@Slf4j
@RequestMapping("/api/LoginApiController")
public class LoginApiController {
    @Autowired
    SysUserService sysUserService;
    @Autowired
    SimpleCharVerifyCodeGenService simpleCharVerifyCodeGenService;
    @Autowired
    RedisUtil redisUtil;

    /**
     * 后台登录接口
     * @param param 接收传来的参数集合
     * @return
     */
    @PostMapping("login")
    public void login(@RequestBody Map<String,Object> param,HttpServletResponse response,SysUserVO sysUserVO) throws Exception {
        sysUserVO = Result.mapToBean(param,SysUserVO.class);
        SysUserVO sysUserVOs = null;
        try {
            /*String VerifyCode1 = (String) request.getSession().getAttribute("VerifyCode");
            String VerifyCode =  request.getParameter("VerifyCode");
            if (!VerifyCode.toLowerCase().equals(VerifyCode1.toLowerCase())){
                return Result.responeAjax(false, "验证码错误", null);
            }*/
            // 密码加密
            sysUserVO.setPassword(PasswordUtil.encrypt(sysUserVO.getPassword(), sysUserVO.getUsername(), PasswordUtil.getStaticSalt()));
            // 获取用户
            sysUserVOs = sysUserService.getUserByUsername(sysUserVO.getUsername());
            if (sysUserVOs != null) {
                if (!sysUserVOs.getPassword().equals(sysUserVO.getPassword())) {
                    Result.print(response,Result.responeAjaxMap(false, "用户名或密码错误,请重新输入", null, null));
                }else if (sysUserVOs.getUserType()==1){
                    Result.print(response,Result.responeAjaxMap(false, "帐号被锁定，请联系管理员", null, null));
                }
            }else {
                Result.print(response,Result.responeAjaxMap(false, "用户名或密码错误,请重新输入", null, null));
            }
            String token = TokenUtil.getToken(sysUserVO.getUsername());
            Map<String,Object> map = new HashMap<>();
            map.put("token",token);
            // 键id 值为token
            redisUtil.set(sysUserVOs.getId(),token);
            Result.print(response, Result.responeAjaxMap(true, "登录成功", sysUserVOs,map));
        } catch (Exception e) {
            Result.print(response, Result.responeAjaxMap(false, e.getMessage(), null, null));
        }
    }

    /**
     * 新增用户
     *
     * @param param 接收传来的参数集合
     * @return
     */
    @PostMapping("register")
    public void register(@RequestBody Map<String,Object> param, HttpServletResponse response,SysUserVO sysUserVO, SysUserInfomationVO sysUserInfomationVO) throws Exception {
        sysUserVO = Result.mapToBean(param,SysUserVO.class);
        sysUserInfomationVO = Result.mapToBean(param,SysUserInfomationVO.class);
        SysUserVO loginuser = sysUserService.getUserByUsername(sysUserVO.getUsername());
        if (loginuser != null) {
            Result.print(response, Result.responeAjaxMap(false, "账号已存在", null, null));
        }else {
            //密码进行md5加密
            sysUserVO.setPassword(PasswordUtil.encrypt(sysUserVO.getPassword(), sysUserVO.getUsername(), PasswordUtil.getStaticSalt()));
            SysRoleVO sysRoleVO = new SysRoleVO();
            //赋予默认id
            sysRoleVO.setId("e6d2f46939c611ea8e185254009a012e");
            sysUserVO.setSysRoleVO(sysRoleVO);
            boolean result = sysUserService.insertUser(sysUserVO, sysUserInfomationVO);
            sysUserVO.setSysUserInfomationVO(sysUserInfomationVO);
            String token = TokenUtil.getToken(sysUserVO.getUsername());
            Map<String,Object> map = new HashMap<>();
            map.put("token",token);
            // 键id 值为token
            redisUtil.set(sysUserVO.getId(),token);
            Result.print(response, Result.responeAjaxMap(result, result ? "注册成功" : "注册失败", sysUserVO, map));
        }
    }
}
